package com.med.controller;

import com.med.dto.RespondDTO;
import com.med.entity.AppointmentDO;
import com.med.entity.DoctorDO;
import com.med.service.IAppointmentService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: internet-based-healthcare
 * @BelongsPackage: com.med.controller
 * @Author: 小吴
 * @CreateTime: 2025-08-21 16:29
 * @Description: 门诊预约控制器
 * @Version: 1.0
 */
@RestController
@RequestMapping("/appointment")
@RefreshScope
public class AppointmentController extends BaseController {

    @Autowired
    private IAppointmentService appointmentService;

    /**
     * @Description: 门诊预约导航---用户端
     * @Param:
     * @return:
     */
    @RequestMapping("/queryDepartment")
    public RespondDTO queryDepartment() {
        RespondDTO queryDepartment = appointmentService.queryDepartment();
        return queryDepartment;
    }


    //根据部门id查询医生
    @GetMapping("/selectDoctor")
    public R<List<DoctorDO>> selectDoctor(@RequestParam Integer subItemId) {
        List<DoctorDO> list = appointmentService.queryDoctor(subItemId);
        return R.ok(list);
    }

    //根据医生id查找该医生的可预约时间
    @GetMapping("/selectAppointment")
    public R<List<AppointmentDO>> selectAppointment(@RequestParam Integer doctorId) {
        List<AppointmentDO> list = appointmentService.queryAppointment(doctorId);
        return R.ok(list);
    }

    // 创建预约（锁定时段）
    @PostMapping("/create")
    public RespondDTO createAppointment(
            @RequestBody Map<String, Integer> params  // 用Map接收请求体参数
    ) {
        // 从Map中获取参数
        Integer appointmentId = params.get("appointmentId");
        Integer userId = params.get("userId");

        // 简单校验参数
        if (appointmentId == null || userId == null) {
            return RespondDTO.fail("参数不完整");
        }

        return appointmentService.createAppointment(appointmentId, userId);
    }


    //确认支付
    @PostMapping("/confirmPayment")
    public RespondDTO confirmPayment(
            @RequestParam Integer appointmentId,
            @RequestParam Integer userId
    ) {
        return appointmentService.confirmPayment(appointmentId, userId);
    }
}